import 'package:flutter/material.dart';
import 'package:helloworld/login.dart';
import 'package:helloworld/pages/debt.dart';
import 'package:helloworld/pages/deposit.dart';
import 'package:helloworld/pages/goal.dart';
import 'package:helloworld/pages/pay.dart';
import 'dart:ui';
import 'package:dio/dio.dart';
import 'package:helloworld/res/incomeData.dart';

class Tabs extends StatefulWidget {
  int _index = 0;
  Tabs({index}) {
    if (index != null) _index = index;
  }
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _Tabs(_index);
  }
}

class _Tabs extends State<Tabs> {
  int _index;
  int _listIndex;
  GlobalKey<ScaffoldState> _globalKeyState = GlobalKey();
  List _list = [Pay(), Deposit(), Debt(), Goal()];
  _Tabs(int index) {
    _index = index;
    if (index > 2) {
      _listIndex = index - 1;
    } else {
      _listIndex = index;
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      key: _globalKeyState,
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: Container(
        // decoration: BoxDecoration(
        //   borderRadius: BorderRadius.circular(150),
        //   border: Border.all(width: 0.1,color: Colors.grey),
        // ),
        padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
        height: 60,
        width: 60,
        child: FloatingActionButton(
          elevation: 2,
          child: Icon(Icons.add, size: 30),
          onPressed: () {
            setState(() {
              _index = 2;
            });
            Navigator.pushNamed(context, '/addPay');
          },
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        onTap: (int index) {
          if (index == 2) {
            setState(() {
              _index = index;
            });
            Navigator.pushNamed(context, '/addPay');
          } else if (index > 2) {
            setState(() {
              _listIndex = index - 1;
              _index = index;
            });
          } else {
            setState(() {
              _listIndex = index;
              _index = index;
            });
          }
        },
        currentIndex: _index,
        items: [
          BottomNavigationBarItem(
              title: Text('支出'), icon: Icon(Icons.add_shopping_cart)),
          BottomNavigationBarItem(title: Text('存款'), icon: Icon(Icons.print)),
          BottomNavigationBarItem(
              title: Text('记账'), icon: Icon(Icons.account_box)),
          BottomNavigationBarItem(
              title: Text('负债'), icon: Icon(Icons.strikethrough_s)),
          BottomNavigationBarItem(title: Text('目标'), icon: Icon(Icons.today))
        ],
      ),
      appBar: AppBar(
        elevation: 0, //记得来改
        leading: InkWell(
          child: Container(
            padding: EdgeInsets.all(10),
            child: CircleAvatar(
              backgroundImage: NetworkImage(
                  "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2403972683,323210450&fm=11&gp=0.jpg"),
              radius: 150,
            ),
          ),
          onTap: () {
            if (_globalKeyState.currentState.isDrawerOpen) {
              _globalKeyState.currentState.openEndDrawer();
            } else {
              _globalKeyState.currentState.openDrawer();
            }
          },
        ),
        title: Text('奶呆记账'),
      ),
      drawer: Drawer(
        child: Column(
          children: <Widget>[
            Container(
                width: double.infinity,
                height: 190,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(
                      height: 40,
                    ),
                    Container(
                      // padding: EdgeInsets.all(20),
                      child: CircleAvatar(
                        backgroundImage: NetworkImage(
                            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2403972683,323210450&fm=11&gp=0.jpg"),
                      ),
                      width: 80,
                      height: 80,
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Text(
                      '臭奶呆',
                      style: TextStyle(fontSize: 20),
                    )
                  ],
                )),
            Divider(),
            SizedBox(
              height: 500,
            ),
            Center(
              child: Container(
                padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    border: Border.all(color: Colors.black38, width: 1)),
                child: InkWell(
                  child: Text(
                    "退出登录",
                    style: TextStyle(color: Colors.red, fontSize: 20),
                  ),
                  onTap: () {
                    Navigator.of(context).pop();
                    Navigator.of(context).pushAndRemoveUntil(
                        new MaterialPageRoute(
                            builder: (context) => new Login()),
                        (route) => route == null);
                  },
                ),
              ),
            )
          ],
        ),
      ),
      body: _list[_listIndex],
    );
  }
}
